package cn.thoughtworks.school.services;

import cn.thoughtworks.school.entities.QuizSuggestion;
import cn.thoughtworks.school.entities.User;
import cn.thoughtworks.school.handlers.BusinessException;
import cn.thoughtworks.school.repositories.QuizSuggestionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuizSuggestionService {
    @Autowired
    private QuizSuggestionRepository quizSuggestionRepository;
    @Autowired
    private UserCenterService userCenterService;

    public void submitQuizSuggestion(QuizSuggestion quizSuggestion) {
        quizSuggestionRepository.save(quizSuggestion);
    }

    public List<QuizSuggestion> getQuizSuggestionByOrganization(User user) {
        Long userCurrentOrganizationId = userCenterService.getUserCurrentOrganizationId(user.getId());

        return quizSuggestionRepository.findByOrganizationIdAndSolvedIsFalse(userCurrentOrganizationId);
    }

    public void markAsSolvedById(Long id) throws BusinessException {
        QuizSuggestion quizSuggestion = quizSuggestionRepository.findById(id).orElseThrow(() -> new BusinessException("Current quiz suggestion is not exist"));
        quizSuggestion.markAsSolved();
        quizSuggestionRepository.save(quizSuggestion);
    }
}
